package engine.display.newAttack.action.effect
{
	import com.greensock.TweenMax;
	import com.greensock.easing.Linear;
	
	import engine.logic.SnowBall;
	import engine.logic.SnowLogic;
	import engine.pool.IResoule;
	import engine.pool.ObjectPool;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	
	/**
	 * 雪人逻辑 
	 * @author gf
	 * 
	 */	
	
	public class AttackTrack
	{
		private var space				:Sprite	=	null;
		
		private var _skin				:MovieClip;
		
		private var _birthPoint			:Point;
		
		private var _deathPoint			:Point;
		
		private var _speed				:int;
		
		private var _onPlay				:Function	=	null;
		
		private var _onPlayOver			:Function	=	null;
		
		private var needCatch				:Boolean	=	false;
		
		private var needBomb				:Boolean	=	false;
		
		private var _curveVal			:Number		=	0;
		
		public function AttackTrack(	 effectSpace	:Sprite,
										 effectSkin		:MovieClip,
										 birthPoint		:Point,
										 deathPoint		:Point,
										 target			:DisplayObject,
										 speed			:int,
										 curveVal		:Number 	= 0,
										 isCatch		:Boolean	=false,
										 isBomb			:Boolean	=true,
										 onPlay			:Function	=	null,
										 onPlayOver		:Function	=	null)
		{
			if(null	==	effectSkin)
			{
				return;
			}	
			
			space			=	effectSpace;
			_skin			=	effectSkin;
			_birthPoint		=	birthPoint;
			_deathPoint		=	deathPoint;			
			_speed			=	speed;
			
			_curveVal		=	curveVal;
			_onPlay			=	onPlay;
			_onPlayOver		=	onPlayOver;
			
			(_skin as SnowBall).playEffect("点燃");
			(_skin as SnowBall).target = target;
			
			needCatch			=	isCatch;
			needBomb			=	isBomb;
			
			init();
		}
		
		private function init():void
		{
			_skin.addEventListener(Event.ADDED_TO_STAGE,onHeadAdd);
			space.addChild(_skin);
		}
		
		private function onHeadAdd(e:Event):void
		{
			_skin.removeEventListener(Event.ADDED_TO_STAGE,onHeadAdd);
			
			createEffect();
		}
		
		private function createEffect():void
		{
			_skin.x		=	_birthPoint.x;
			_skin.y		=	_birthPoint.y;
			
			var distance	:int	=	int(Point.distance(_birthPoint,_deathPoint));
			
			var time		:Number	=	int(1000*distance/_speed)*0.001;
			
			var h			:Number	=	distance*_curveVal;
			
			var centerX:Number		=	(_birthPoint.x+_deathPoint.x)*.5
			var centerY:Number		=	(_birthPoint.y+_deathPoint.y)*.5
			
			var angle		:Number = Math.atan(-(_birthPoint.y-_deathPoint.y)/(_birthPoint.x-_deathPoint.x));
			var realangle	:Number = Math.PI / 2.0 - angle;
			
			var tagetX		:int = int(centerX - h * Math.cos(realangle));
			var tagetY		:int = int(centerY - h * Math.sin(realangle));
			
			var posArr:Array	=	needCatch?[{x:tagetX,y:tagetY}]:[{x:tagetX,y:tagetY},{x:_deathPoint.x,y:_deathPoint.y}];
			
			TweenMax.to(_skin, time, {bezier:posArr, orientToBezier:true, ease:Linear.easeNone,onUpdate:onEffectPlay,onComplete:onEffectPlayerOver});
		}
		
		private function onEffectPlay():void
		{
			if(_onPlay!=null)
			{
				_onPlay.call(this,this);
			}
		}
		
		
		private function onEffectPlayerOver():void
		{
			if(!needBomb)
			{
				ObjectPool.GetPool(SnowBall).returnObject(_skin as IResoule);
				clearEffect();
				return;
			}
			if(!needCatch)
			{
				(_skin as SnowBall).playEffect("爆炸");
				(_skin as SnowBall).Ball.addFrameScript((_skin as SnowBall).Ball.totalFrames-1,endEffect);
				function endEffect():void
				{
					ObjectPool.GetPool(SnowBall).returnObject(_skin as IResoule);
					clearEffect();
				}
			}
			else
			{
				(_skin as SnowBall).startCatch();
				(_skin as SnowBall).endCatchFunc	=	function():void
				{
					ObjectPool.GetPool(SnowBall).returnObject(_skin as IResoule);
					clearEffect();
				}
			}
		}
		
		
		private function clearEffect():void
		{
			SnowLogic.getInstance().isBomb=false;
			if(_onPlayOver!=null)
			{			
				_onPlayOver.call(this,this);
			}
			
			if(_skin != null)
			{
				TweenMax.killChildTweensOf(_skin,true);
				
				if(_skin.parent != null)
				{
					_skin.parent.removeChild(_skin);
				}
			}
			_birthPoint		=	null;
			_deathPoint		=	null;
			_skin			= 	null;
			_onPlay			=	null;
			_onPlayOver		=	null;
		}
	}
}